package cn.jimmie.learn.viewArt.weight;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import cn.jimmie.learn.viewArt.R;

/**
 * function :
 *
 * <p> Create Time: 2018年12月23日 22:06 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class FadedCircleLoadingView extends View implements ValueAnimator.AnimatorUpdateListener {
    private static final float SPEED = 3;

    private Paint mDotPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
    private ValueAnimator mAnimator = ValueAnimator.ofInt(0, 36000);

    private int mAngle = 0;
    private RectF mRect = new RectF();
    private float mStrokeWidth;
    private float mPadding;
    private float mSpeed = SPEED * 1.5f;

    public FadedCircleLoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FadedCircleLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        int[] attrsArray = new int[]{android.R.attr.layout_width, android.R.attr.layout_height};
        TypedArray sta = context.obtainStyledAttributes(attrs, attrsArray);
        float width = sta.getDimension(0, ViewGroup.LayoutParams.MATCH_PARENT);
        float height = sta.getDimension(1, ViewGroup.LayoutParams.MATCH_PARENT);
        sta.recycle();

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FadedCircleLoadingView);
        mPadding = ta.getDimension(R.styleable.FadedCircleLoadingView_android_padding, 0);
        float realWidth = Math.min(width, height) - mPadding;

        float innerRadius = ta.getDimension(R.styleable.FadedCircleLoadingView_innerRadius, realWidth * 3 / 10);
        float outerRadius = ta.getDimension(R.styleable.FadedCircleLoadingView_outRadius, realWidth / 2);
        int startColor = ta.getColor(R.styleable.FadedCircleLoadingView_startColor, Color.WHITE);
        int endColor = ta.getColor(R.styleable.FadedCircleLoadingView_endColor, Color.parseColor("#ff9515"));
        ta.recycle();

        mStrokeWidth = outerRadius - innerRadius;

        mRect.set(mStrokeWidth / 2 + mPadding, mStrokeWidth / 2 + mPadding, width - mStrokeWidth / 2 - mPadding, height - mStrokeWidth / 2 - mPadding);

        mPaint.setColor(startColor);
        mDotPaint.setColor(endColor);
        mPaint.setStrokeWidth(mStrokeWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        SweepGradient gradient = new SweepGradient(width / 2, height / 2, startColor, endColor);
        mPaint.setShader(gradient);

        mAnimator.setInterpolator(new LinearInterpolator());
        mAnimator.setRepeatCount(10000);
        mAnimator.setDuration(500000);
        mAnimator.addUpdateListener(this);
        mAnimator.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();
        canvas.rotate(-mAngle, getWidth() / 2, getHeight() / 2);
        canvas.drawArc(mRect, 0, 360, false, mPaint);
        canvas.drawCircle(getWidth() - mStrokeWidth / 2 - mPadding, getHeight() / 2, mStrokeWidth / 2, mDotPaint);
        canvas.restore();
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        mAngle = -(int) ((int) animation.getAnimatedValue() * mSpeed % 360);
        invalidate();
    }

    @Override
    protected void onDetachedFromWindow() {
        mAnimator.cancel();
        super.onDetachedFromWindow();
    }

    @Override
    public void setVisibility(int visibility) {
        mAnimator.cancel();
        if (visibility == View.VISIBLE) {
            mAnimator.start();
        }
        super.setVisibility(visibility);
    }
}
